package com.css.fxfzypg.modules.earthquakedisasterloss.controller;

import cn.hutool.core.lang.hash.Hash;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.earthquakedisasterloss.entity.TrafficAnalysis;
import com.css.fxfzypg.modules.earthquakedisasterloss.entity.YpgResultRoad;
import com.css.fxfzypg.modules.earthquakedisasterloss.service.CountLandslideService;
import com.css.fxfzypg.modules.earthquakedisasterloss.vo.VersionPageVO;
import com.css.fxfzypg.modules.hypergraph.service.HypergraphService;
import com.css.fxfzypg.modules.maps.service.CountLandslideMapService;
import com.css.fxfzypg.util.JdbcUtil;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: fxfzypg
 * @Author: lhl
 * @CreateDate: 2022/10/28 18:05
 */
@RestController
@Slf4j
@RequestMapping("/count")
public class CountLandslideController {

    @Resource
    CountLandslideService countLandslideService;

    @Resource
    CountLandslideMapService countLandslideMapService;//地图使用
    @Resource
    HypergraphService hypergraphService;

    /**
     * 逻辑斯蒂计算评估
     */
    @PostMapping("/countLandslide")
    public RestResponse count1(@RequestBody Map<String,Object> map) {
        RestResponse restResponse = null;
        try {
            //刷新指定超图工作空间数据源数据集
            hypergraphService.getWorkspaceReload("ypg_preassess_task");
            //刷新指定超图工作空间数据源数据集
            hypergraphService.getWorkspaceReload("ypgTifCount","");
            String taskId = (String) map.get("taskId");
            String parameter = map.get("parameter")+"";
            String dzIntensity = (String) map.get("dzIntensity");
            //烈度6-11
            switch (dzIntensity) {
                case "Ⅵ":
                    dzIntensity = String.valueOf(6);
                    break;
                case "Ⅶ":
                    dzIntensity = String.valueOf(7);
                    break;
                case "Ⅷ":
                    dzIntensity = String.valueOf(8);
                    break;
                case "Ⅸ":
                    dzIntensity = String.valueOf(9);
                    break;
                case "Ⅹ":
                    dzIntensity = String.valueOf(10);
                    break;
                case "Ⅺ":
                    dzIntensity = String.valueOf(11);
                    break;
            }
            restResponse = countLandslideService.count1(taskId, (String) map.get("slopeVersion"),
                    dzIntensity, parameter, (String) map.get("tab"));

            //根据任务ID删除缓冲区内相交部分的道路数据
//            countLandslideMapService.delYpgResultRoad(taskId);
            //根据任务ID、烈度删除缓冲区内相交部分的道路数据
            countLandslideMapService.delYpgResultRoadIntensity(taskId,dzIntensity);
            if (restResponse.getCode() == 200) {
                //保存缓冲区内相交部分的道路数据
                countLandslideMapService.saveResultRoad(taskId, parameter,dzIntensity);
                //刷新指定超图工作空间数据源数据集
                hypergraphService.getWorkspaceReload("ypg_result_road_new");
                //刷新指定超图工作空间数据源数据集
//            hypergraphService.getWorkspaceReload("ypgTifCount","");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"计算失败！");
        }
        return restResponse;
    }

    /**
     * 黄土模型计算评估
     */
    @PostMapping("/count2")
    public RestResponse count2(@RequestBody Map<String,Object> map) {
        String taskId =(String) map.get("taskId");
        //烈度
        String dzIntensity =(String) map.get("dzIntensity");
        //考虑对道路影响的范围
        String parameter =(String) map.get("parameter");
        //坡度
        String slopeVersion =(String) map.get("slopeVersion");
        //地层岩性
        String lithologyVersion =(String) map.get("lithologyVersion");
        //地貌
        String phyVersion =(String) map.get("phyVersion");
        //天然密度
        String natDenVersion =(String) map.get("natDenVersion");
        RestResponse restResponse = null;
        try {
            //刷新指定超图工作空间数据源数据集
            hypergraphService.getWorkspaceReload("ypg_preassess_task");
            //刷新指定超图工作空间数据源数据集
            hypergraphService.getWorkspaceReload("ypgTifCount","");
            //烈度6-11
            switch (dzIntensity) {
                case "Ⅵ":
                    dzIntensity = String.valueOf(6);
                    break;
                case "Ⅶ":
                    dzIntensity = String.valueOf(7);
                    break;
                case "Ⅷ":
                    dzIntensity = String.valueOf(8);
                    break;
                case "Ⅸ":
                    dzIntensity = String.valueOf(9);
                    break;
                case "Ⅹ":
                    dzIntensity = String.valueOf(10);
                    break;
            }
            restResponse = countLandslideService.count2(taskId, dzIntensity, parameter, slopeVersion, lithologyVersion, phyVersion
                    , natDenVersion);

            //根据任务ID删除缓冲区内相交部分的道路数据
//            countLandslideMapService.delYpgResultRoad(taskId);
            //根据任务ID、烈度删除缓冲区内相交部分的道路数据
            countLandslideMapService.delYpgResultRoadIntensity(taskId,dzIntensity);
            if (restResponse.getCode() == 200) {
                //保存缓冲区内相交部分的道路数据
                countLandslideMapService.saveResultRoad(taskId, parameter,dzIntensity);
                //刷新指定超图工作空间数据源数据集
                hypergraphService.getWorkspaceReload("ypg_result_road_new");
                //刷新指定超图工作空间数据源数据集
//            hypergraphService.getWorkspaceReload("ypgTifCount","");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"计算失败！");
        }
        return restResponse;
    }
    /**
     * 业务数据分页条件查询接口
     * 获取坡度数据版本
     * @param map
     *  dataTypeLevel3 坡度固定值  dxdmpd
     *  distinct 任务地区
     *  uploadStartTime 上传时间开始时间
     *  uploadEndTime 上传时间结束时间
     *  versionDesc 版本说明
     *  uploadUsername 上传人用户名
     * */
    @GetMapping("/getParam0Version")
    public RestResponse getParam0Version(@RequestParam Map<String,Object> map) {
        return countLandslideService.getParam0Version(map);
    }

    //根据任务ID查询缓冲区内相交部分的道路数据、滑坡计算结果网格
    @PostMapping("/getYpgResultSlopeRoadByTaskId")
    public RestResponse getYpgResultSlopeRoadByTaskId(@RequestParam String taskId,@RequestParam String dzIntensity) {
        RestResponse restResponse = null;
        try {
            List<Object> list = new ArrayList<Object>();
            //根据任务ID、烈度查询缓冲区内相交部分的道路数据
            list.add(countLandslideMapService.getYpgResultRoadByTaskId(taskId,dzIntensity));
            //根据任务ID查询滑坡计算结果网格
//            list.add(countLandslideMapService.getYpgResultSlopeByTaskId(taskId));
            //根据任务ID、烈度查询计算结果图层信息列表（图层数据类型：最终计算、分级矢量化、缓冲区4级、缓冲区5级）
            list.add(countLandslideMapService.getYpgResultSlopeNewByTaskId(taskId,dzIntensity));
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"查询失败！");
        }
        return restResponse;
    }

    //根据任务ID查询滑坡缓冲区
    @PostMapping("/getYpgResultSlopeBufferByTaskId")
    public RestResponse getYpgResultSlopeBufferByTaskId(@RequestParam String taskId) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(countLandslideMapService.getYpgResultSlopeBufferByTaskId(taskId));
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"查询失败！");
        }
        return restResponse;
    }

    //根据任务ID查询缓冲区内相交部分的道路数据统计结果
    @PostMapping("/getYpgResultRoadListByTaskId")
    public RestResponse getYpgResultRoadListByTaskId(@RequestParam String taskId,@RequestParam String dzIntensity) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(countLandslideMapService.getYpgResultRoadListByTaskId(taskId,dzIntensity));
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"查询失败！");
        }
        return restResponse;
    }

    @GetMapping("/getTrafficAnalysisByTaskId")
    public RestResponse getTrafficAnalysisByTaskId(@RequestParam String taskId,@RequestParam String dzIntensity) {
        RestResponse restResponse = null;
        try {
            restResponse = countLandslideService.getTrafficAnalysisByTaskId(taskId,dzIntensity);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"查询失败！");
        }
        return restResponse;
    }

    @PostMapping("/saveTrafficAnalysis")
    public RestResponse saveTrafficAnalysis(@RequestBody TrafficAnalysis entity) {
        RestResponse restResponse = null;
        try {
            restResponse = countLandslideService.saveTrafficAnalysis(entity);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"保存失败！");
        }
        return restResponse;
    }

    @PostMapping("/updateTrafficAnalysis")
    public RestResponse updateTrafficAnalysis(@RequestBody TrafficAnalysis entity) {
        RestResponse restResponse = null;
        try {
            restResponse = countLandslideService.updateTrafficAnalysis(entity);
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"修改失败！");
        }
        return restResponse;
    }

    //根据任务ID查询全部烈度计算结果列表
    @GetMapping("/getYpgResultSlopeNewIntensityList")
    public RestResponse getYpgResultSlopeNewIntensityList(@RequestParam String taskId) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(countLandslideMapService.getYpgResultSlopeNewIntensityList(taskId));
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail(503,"查询失败！");
        }
        return restResponse;
    }
}
